//
//  BCSData.swift
//  BCFoundation
//
//  Created by Basic on 2019/10/14.
//  Data 扩展

import Foundation

public extension Data {

    
    /// data 转 dictonary
    func bc_toDict() -> Dictionary<String, Any>? {
        do {
            let json = try JSONSerialization.jsonObject(with: self, options: .mutableContainers)
            let dic = json as? Dictionary<String, Any>
            return dic
        } catch _ {
            return nil
        }
    }
    /// data 转 string
    func bc_toString() -> String {
        let dataStr = String(data: self, encoding: String.Encoding.utf8) ?? ""
        return dataStr
    }
    /// data 转 hex string
    func bc_toHexString() -> String {
        if self.count == 0 { return "" }
        return self.map { String(format: "%02x", $0) }.joined(separator: "")
    }

}
